<%@ page import="cake.Cake" %>
<%@ page import="java.util.List" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.mchange.v2.resourcepool.NoGoodResourcesException" %>
<%@ page import="java.text.DecimalFormat" %>

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2023-5-21
  Time: 14:35
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<html>
<head>
    <title>我的购物车</title>
</head>
<body>
<script src="cart%20num.js"></script>
<div style="text-align: center">
    <%
        ArrayList buylist=(ArrayList) session.getAttribute("buylist");
        float sum=0;
    %>

    <div>
        <h1>购物车</h1>
        <hr width=1300 size=0 color="#999999">
    </div>
</div>

<p><b>尊敬的用户：</b></p>
<p>您选购的订单详情如下</p>
<table width="600" border="1" cellpadding="0" align="center">
    <thead>
    <tr>
        <th>序号</th>
        <th>商品名称</th>
        <th>单价</th>
        <th>数量</th>
        <th>价格</th>
    </tr>
    <% if(buylist==null||buylist.size()==0){ %>
    <tr height="100px"><td align="center" colspan="5">购物车为空！</td></tr>
    <% }
    else {
        for (int i=0;i<buylist.size();i++){
            Cake good=(Cake) buylist.get(i);
            String name= good.getShop();
            Double price= Double.valueOf(good.getPrice());
            int num= Integer.parseInt(good.getNum());
            String money=new DecimalFormat("#.00").format(num*1.0*price);
            sum+=Double.parseDouble(money);

        }
    }
    %>
    <%
        String id = request.getParameter("id");
        String driverClass = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/cart?serverTimezone=Asia/Shanghai";
        String user = "root";
        String password = "276413";
        Connection conn = null;
        ResultSet rs = null;
        Statement stmt = null;
        try {
            Class.forName(driverClass);
            conn = DriverManager.getConnection(url, user, password);
            stmt = conn.createStatement();
            String sql = "select * from shop where id='" + id + "'";
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
    %>
    <tr align="center">
        <td><%=rs.getString("id")%>
        </td>
        <td><%=rs.getString("shop")%>
        </td>
        <td><%=rs.getString("price")%>
        </td>
        <td><%=rs.getString("num")%>
            <div class="p-quantity">
                <input type="button" class="decrease" value="-">
                <input type="text" class="quantity" value="1"/>
                <input type="button" class="increase" value="+">
            </div>

        </td>
        <td><%=rs.getString("money")%></td>
    </tr>
    <%
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    %>
    </tr>
    </thead>

</table>
<div style="text-align: center">
    <br>
    <div>总金额为：<%=sum%></div>
    <br>
    <br>
    <a href="cart.jsp"><button>继续购物</button></a>
        <a href="clear.jsp"><button>清空购物车</button></a>

</div>

</body>
</html>